package com.tinycloud.miracll.product.service.impl;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.core.utils.StringUtils;
import com.tinycloud.miracll.product.domain.*;
import com.tinycloud.miracll.product.domain.json.ScadaTagLogValues;
import com.tinycloud.miracll.product.service.*;
import com.tinycloud.miracll.product.util.ScadaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 重控点位报警service接口实现类
 *
 * @author tiny
 * @date 2021-12-07
 */
@Service
public class ProductKeyProcessDeviateAlarmServiceImpl   implements IProductKeyProcessDeviateAlarmService {

    @Autowired
    IScadaAlarmService scadaAlarmService;

    @Autowired
    IDevicestatsService devicestatsService;

    @Autowired
    IProductKeyProcessAlarmService productKeyProcessAlarmService;

    @Autowired
    IProductKeyProcessDeviateConfDetailService productKeyProcessDeviateConfDetailService;

    @Override
    public int cleanScadaAlarmData() {

        List<ScadaAlarm> scadaAlarmList =  scadaAlarmService.getNoCleanScadaAlarmList();
        for (ScadaAlarm scadaAlarm : scadaAlarmList) {
            try {
                String alarmTagName = scadaAlarm.getTagName();
                ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetail = new ProductKeyProcessDeviateConfDetail();
                productKeyProcessDeviateConfDetail.setAlarmPoint(alarmTagName);
                scadaAlarm.setDataCleanTime(DateUtils.getNowDate());
                scadaAlarmService.updateScadaAlarm(scadaAlarm);
                List<ProductKeyProcessDeviateConfDetail>  productKeyProcessDeviateConfDetailList = productKeyProcessDeviateConfDetailService.selectProductKeyProcessDeviateConfDetailList(productKeyProcessDeviateConfDetail);
                if (null!=productKeyProcessDeviateConfDetailList&&productKeyProcessDeviateConfDetailList.size()>0) {
                    ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetailTem = productKeyProcessDeviateConfDetailList.get(0);
                    System.out.println(productKeyProcessDeviateConfDetailTem.getMeasurePoint());
                    ProductKeyProcessAlarm productKeyProcessAlarm = this.genProductKeyProcessAlarm(scadaAlarm, productKeyProcessDeviateConfDetailTem);
                    System.out.println("---------AlarmPoint:-----------"+productKeyProcessAlarm.getAlarmPoint());
                    System.out.println("---------AlarmDesc:-----------"+productKeyProcessAlarm.getAlarmDesc());
                    System.out.println("---------AlarmDescCus:-----------"+productKeyProcessAlarm.getAlarmDescCus());
                    System.out.println("---------scadaAction:-----------"+scadaAlarm.getAction());
                    productKeyProcessAlarmService.insertProductKeyProcessAlarm(productKeyProcessAlarm);

                }else {
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return 0;

    }

    private ProductKeyProcessAlarm genProductKeyProcessAlarm(ScadaAlarm scadaAlarm,ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetail) {

        ProductKeyProcessAlarm productKeyProcessAlarm = new ProductKeyProcessAlarm();
        Date alarmDate = scadaAlarm.getLogTimeAll();
       /* alarmDate = DateUtils.addSeconds(alarmDate,1);*/
        BigDecimal alarmValue = scadaAlarm.getAlarmValue();
        BigDecimal alarmLimit = scadaAlarm.getAlarmLimit();
        String alarmPoint = productKeyProcessDeviateConfDetail.getAlarmPoint().trim();
        String issuePoint = productKeyProcessDeviateConfDetail.getIssuePoint().trim();
        String measurePoint = productKeyProcessDeviateConfDetail.getMeasurePoint().trim();
        String calculatePoint = productKeyProcessDeviateConfDetail.getCalculatePoint().trim();
        String lineCode =  productKeyProcessDeviateConfDetail.getLine();
        Long erpAlarmType = productKeyProcessDeviateConfDetail.getAlarmNameType();
        List<String> tagNames = new ArrayList<>();

        tagNames.add(issuePoint);
        tagNames.add(measurePoint);
        tagNames.add(calculatePoint);

        productKeyProcessAlarm.setAlarmPoint(alarmPoint);
        productKeyProcessAlarm.setIssuePoint(issuePoint);
        productKeyProcessAlarm.setMeasurePoint(measurePoint);
        productKeyProcessAlarm.setCalculatePoint(calculatePoint);
        productKeyProcessAlarm.setAlarmValue(alarmValue);
        productKeyProcessAlarm.setAlarmLimit(alarmLimit);
        productKeyProcessAlarm.setLineCode(lineCode);
        productKeyProcessAlarm.setErpAlarmType(erpAlarmType.toString());

        String beginTimeString = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,alarmDate);
        List<ScadaTagLogValues> scadaTagLogValuesList = ScadaUtil.getScadaTagValueLog(tagNames,beginTimeString,1,1);
        for (ScadaTagLogValues scadaTagLogValuesTem : scadaTagLogValuesList) {
            String scadaValueTem = scadaTagLogValuesTem.getValues().get(0);
            String[] strArray={"","#","*"};
            if (StringUtils.inStringIgnoreCase(scadaValueTem,strArray)) {
                scadaValueTem = "-999";
            }
            if (productKeyProcessAlarm.getIssuePoint().equals(scadaTagLogValuesTem.getName())) {

                productKeyProcessAlarm.setIssueValue(new BigDecimal(scadaValueTem));

            }else if (productKeyProcessAlarm.getMeasurePoint().equals(scadaTagLogValuesTem.getName())) {

                productKeyProcessAlarm.setMeasureValue(new BigDecimal(scadaValueTem));
            }else if(productKeyProcessAlarm.getCalculatePoint().equals(scadaTagLogValuesTem.getName())) {

                productKeyProcessAlarm.setCalculateValue(new BigDecimal(scadaValueTem));
            }

        }

        beginTimeString = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.addSeconds(alarmDate,6));
        tagNames = new ArrayList<>();
        tagNames.add(calculatePoint);
        scadaTagLogValuesList = ScadaUtil.getScadaTagValueLog(tagNames,beginTimeString,1,1);
        for (ScadaTagLogValues scadaTagLogValuesTem : scadaTagLogValuesList) {
            String scadaValueTem = scadaTagLogValuesTem.getValues().get(0);
            String[] strArray={"","#","*"};
            if (StringUtils.inStringIgnoreCase(scadaValueTem,strArray)) {
                scadaValueTem = "-999";
            }
            productKeyProcessAlarm.setCalculateValueNext(new BigDecimal(scadaValueTem));

        }

        if(null==calculatePoint||calculatePoint.length()<=0) {
            productKeyProcessAlarm.setValidFlag(1);
        } else if ( calculatePoint.length()>0 && null!=productKeyProcessAlarm.getCalculateValue() && productKeyProcessAlarm.getCalculateValue().longValue()==productKeyProcessDeviateConfDetail.getCalculatePointValue()){
            productKeyProcessAlarm.setValidFlag(1);
        } else {
            productKeyProcessAlarm.setValidFlag(0);
        }


        productKeyProcessAlarm.setErpAnalFlag(0);
        productKeyProcessAlarm.setErpPushFlag(0);
        productKeyProcessAlarm.setId(UuidUtils.generateUuid());
        productKeyProcessAlarm.setAlarmTime(alarmDate);
//        productKeyProcessAlarm.setIssuePointDesc(productKeyProcessDeviateConfDetail.getArgName());
        productKeyProcessAlarm.setIssuePointDesc(productKeyProcessDeviateConfDetail.getAlarmName());
        productKeyProcessAlarm.setAlarmDesc(scadaAlarm.getAction());
        productKeyProcessAlarm.setCreateTime(DateUtils.getNowDate());

        Devicestats devicestats = new Devicestats();
        devicestats.setDevice(productKeyProcessDeviateConfDetail.getLine());
        devicestats.setState(2L);
        devicestats.setStarttime(productKeyProcessAlarm.getAlarmTime());
        devicestats.setStoptime(productKeyProcessAlarm.getAlarmTime());
        List<Devicestats> devicestatsList = devicestatsService.selectDevicestatsListTimeRange(devicestats);
        if (null!=devicestatsList&&0!=devicestatsList.size()) {
            productKeyProcessAlarm.setBatchNum(devicestatsList.get(0).getBatch());
        }else {
            devicestats = new Devicestats();
            devicestatsList = new ArrayList<Devicestats>();
            devicestats.setDevice(productKeyProcessDeviateConfDetail.getLine());
            devicestats.setState(1L);
            devicestatsList = devicestatsService.selectDevicestatsList(devicestats);
            if (null!=devicestatsList&&0!=devicestatsList.size()) {
                productKeyProcessAlarm.setBatchNum(devicestatsList.get(0).getBatch());
            }
            productKeyProcessAlarm.setScadaalarmid(scadaAlarm.getId());
        }

        if (null==lineCode||lineCode.length()<=0) {
            productKeyProcessAlarm.setBatchNum(null);
        }
        productKeyProcessAlarm  = this.calculateValueCus(productKeyProcessAlarm,productKeyProcessDeviateConfDetail);
        return  productKeyProcessAlarm;

    }

    private ProductKeyProcessAlarm calculateValueCus(ProductKeyProcessAlarm productKeyProcessAlarm,ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetail ) {

        String alarmDesc = productKeyProcessAlarm.getAlarmDesc();

        if(StringUtils.contains(alarmDesc, "- 偏差高 报警")) {
            productKeyProcessAlarm.setAlarmType("1");
            productKeyProcessAlarm.setAlarmDescCus("高上限报警");
        }else  if (StringUtils.contains(alarmDesc, "- 偏差低 报警")) {
            productKeyProcessAlarm.setAlarmType("2");
            productKeyProcessAlarm.setAlarmDescCus("低下限报警");
        }else  if (StringUtils.contains(alarmDesc,"- 高 报警")) {
            productKeyProcessAlarm.setAlarmType("3");
            productKeyProcessAlarm.setAlarmDescCus("高上限报警");
        }else  if (StringUtils.contains(alarmDesc, "- 低 报警")) {
            productKeyProcessAlarm.setAlarmType("4");
            productKeyProcessAlarm.setAlarmDescCus("低下限报警");
        }else  if (StringUtils.contains(alarmDesc, "- 数字量 报警")) {
            productKeyProcessAlarm.setAlarmType("5");
            productKeyProcessAlarm.setAlarmDescCus("状态报警");
        }else  if (StringUtils.contains(alarmDesc, "- 变化率 报警")) {
            productKeyProcessAlarm.setAlarmType("6");
            productKeyProcessAlarm.setAlarmDescCus("变化率报警");
        }else {
            productKeyProcessAlarm.setAlarmType("16");
            productKeyProcessAlarm.setAlarmDescCus("其他类型报警");
        }
        BigDecimal issueValue = productKeyProcessAlarm.getIssueValue();
        BigDecimal issueValueCus = null;
        long alarmTypeERP = productKeyProcessDeviateConfDetail.getAlarmNameType();
        String alarmTypeScada = productKeyProcessAlarm.getAlarmType();
        BigDecimal alarmLimit = productKeyProcessAlarm.getAlarmLimit();
        if (alarmTypeScada=="1") {
            issueValueCus =  alarmLimit.subtract(productKeyProcessDeviateConfDetail.getDeviateHLimit());
            productKeyProcessAlarm.setIssueValueCus(issueValueCus);
        }else  if (alarmTypeScada=="2") {
            issueValueCus = alarmLimit.add(productKeyProcessDeviateConfDetail.getDeviateLLimit());
            productKeyProcessAlarm.setIssueValueCus(issueValueCus);
        } else {
            issueValueCus = issueValue;
            productKeyProcessAlarm.setIssueValueCus(issueValueCus);
        }

        return  productKeyProcessAlarm;
    }

    @Override
    public int productKeyProcessConfERP2Scada() {
        List<ProductKeyProcessDeviateConfDetail> productKeyProcessDeviateConfDetailList = productKeyProcessDeviateConfDetailService.selectProductKeyProcessDeviateConfDetailList(null);

        for (ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetail: productKeyProcessDeviateConfDetailList) {
            List<ScadaSetTagValue> scadaSetTagValueList = new ArrayList<>();
            if (productKeyProcessDeviateConfDetail.getAlarmNameType()==1) {
                scadaSetTagValueList = this.genScadaSetTagValueListDV(productKeyProcessDeviateConfDetail);

            }else if (productKeyProcessDeviateConfDetail.getAlarmNameType()==3) {
                scadaSetTagValueList = this.genScadaSetTagValueListLM(productKeyProcessDeviateConfDetail);

            }
            ScadaUtil.setScadaTagValue(scadaSetTagValueList);
        }

        return 0;
    }


    //偏差
    private  List<ScadaSetTagValue> genScadaSetTagValueListDV(ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetail) {
        List<ScadaSetTagValue> scadaSetTagValueList = new ArrayList<>();

        String alarmPoint = productKeyProcessDeviateConfDetail.getAlarmPoint();
        String deviateHLimit = productKeyProcessDeviateConfDetail.getDeviateHLimit().toString();
        String deviateLLimit = productKeyProcessDeviateConfDetail.getDeviateLLimit().toString();


        //偏差高
        ScadaSetTagValue scadaSetNodeValueADVHI = new ScadaSetTagValue();
        String ADVHIName =alarmPoint+".ADVHI";
        scadaSetNodeValueADVHI.setName(ADVHIName);
        scadaSetNodeValueADVHI.setValue(deviateHLimit);

        //偏差高优先级
        ScadaSetTagValue scadaSetNodeValueAPRHDV = new ScadaSetTagValue();
        String APRHDVName =alarmPoint+".APRHDV";
        scadaSetNodeValueAPRHDV.setName(APRHDVName);
        scadaSetNodeValueAPRHDV.setValue("9");

        //偏差低
        ScadaSetTagValue scadaSetNodeValueADVLO = new ScadaSetTagValue();
        String AADVLOName =alarmPoint+".ADVLO";
        scadaSetNodeValueADVLO.setName(AADVLOName);
//        scadaSetNodeValueADVLO.setName("T-Q2-TI-14-Alarm-Test.ADVLO");
        scadaSetNodeValueADVLO.setValue(deviateLLimit);

        //偏差低优先级
        ScadaSetTagValue scadaSetNodeValueAPRLDV = new ScadaSetTagValue();
        String APRLDVName =alarmPoint+".APRLDV";
        scadaSetNodeValueAPRLDV.setName(APRLDVName);
//        scadaSetNodeValueAPRLDV.setName("T-Q2-TI-14-Alarm-Test.APRLDV");
        scadaSetNodeValueAPRLDV.setValue("9");

        scadaSetTagValueList.add(scadaSetNodeValueADVHI);
        scadaSetTagValueList.add(scadaSetNodeValueAPRHDV);
        scadaSetTagValueList.add(scadaSetNodeValueADVLO);
        scadaSetTagValueList.add(scadaSetNodeValueAPRLDV);

        return  scadaSetTagValueList;

    }

    //上下限
    private  List<ScadaSetTagValue> genScadaSetTagValueListLM(ProductKeyProcessDeviateConfDetail productKeyProcessDeviateConfDetail) {
        List<ScadaSetTagValue> scadaSetTagValueList = new ArrayList<>();
        String alarmPoint = productKeyProcessDeviateConfDetail.getAlarmPoint();
        String hLimit = productKeyProcessDeviateConfDetail.getHighValue().toString();
        String lLimit = productKeyProcessDeviateConfDetail.getLowValue().toString();

        //高
        ScadaSetTagValue scadaSetNodeValueALMHI = new ScadaSetTagValue();
        String ALMHIName =alarmPoint+".ALMHI";
        scadaSetNodeValueALMHI.setName(ALMHIName);
//        scadaSetNodeValueALMHI.setName("T-Q2-TI-14-Alarm-Test.ALMHI");
        scadaSetNodeValueALMHI.setValue(hLimit);

        //高优先级
        ScadaSetTagValue scadaSetNodeValueAPRHI = new ScadaSetTagValue();
        String APRHIName =alarmPoint+".APRHI";
        scadaSetNodeValueAPRHI.setName(APRHIName);
//        scadaSetNodeValueAPRHI.setName("T-Q2-TI-14-Alarm-Test.APRHI");
        scadaSetNodeValueAPRHI.setValue("9");

        //低
        ScadaSetTagValue scadaSetNodeValueALMLO = new ScadaSetTagValue();
        String ALMLOName =alarmPoint+".ALMLO";
        scadaSetNodeValueALMLO.setName(ALMLOName);
//        scadaSetNodeValueALMLO.setName("T-Q2-TI-14-Alarm-Test.ALMLO");
        scadaSetNodeValueALMLO.setValue(lLimit);

        //低优先级
        ScadaSetTagValue scadaSetNodeValueAPRLO = new ScadaSetTagValue();
        String APRLOName =alarmPoint+".APRLO";
        scadaSetNodeValueAPRLO.setName(APRLOName);
//        scadaSetNodeValueAPRLO.setName("T-Q2-TI-14-Alarm-Test.APRLO");
        scadaSetNodeValueAPRLO.setValue("9");

        scadaSetTagValueList.add(scadaSetNodeValueALMHI);
        scadaSetTagValueList.add(scadaSetNodeValueAPRHI);
        scadaSetTagValueList.add(scadaSetNodeValueALMLO);
        scadaSetTagValueList.add(scadaSetNodeValueAPRLO);

        return  scadaSetTagValueList;

    }
}
